angular.module('mamApp').controller('albumController', ['$scope', 'AlbumManager', '$state', '$uibModal', 'ChannelManager', 'CommonManager',
    function ($scope, AlbumManager, $state, $uibModal, ChannelManager, CommonManager) {
        $scope.checkStatus = [{key: "", value: "审核状态"}, {key: 7, value: $scope.table[7]}, {
            key: 8,
            value: $scope.table[8]
        }, {
            key: 9,
            value: $scope.table[9]
        }];


        $scope.fileNames = [{key: "", value: "查找字段"}, {key: "albumId", value: "专辑ID"}, {
            key: "albumName",
            value: "专辑名称"
        }, {key: "userName", value: "账户名"}];

        $scope.adminList = [{adminId: "", fullName: "审核人"}];

        $scope.search = {
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10,
            reviewStatus: $state.params.reviewStatus ? parseInt($state.params.reviewStatus) : "",
            keyword: $state.params.keyword,
            categoryId: $state.params.categoryId ? parseInt($state.params.categoryId) : "",
            channelId: $state.params.channelId ? parseInt($state.params.channelId) : "",
            adminId: $state.params.adminId ? parseInt($state.params.adminId) : "",
            key: $state.params.key || "",
            value: $state.params.value || ""
        };


        $scope.findData = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search);
        }

        AlbumManager.albumsCheckUsers().then(function (result) {
            $scope.adminList = $scope.adminList.concat(result.data.pageList);
        })


        $scope.channels = [{channelId: "", channelName: "二级频道"}];

        if ($scope.search.categoryId) {
            CommonManager.channelList($scope.search.categoryId).then(function (result) {
                $scope.channels = $scope.channels.concat(result.data.pageList)
            })
        }


        CommonManager.categoriesList().then(function (result) {
            $scope.categories = [{categoryId: "", categoryName: "一级频道"}].concat(result.data.pageList);

        })

        $scope.categoryChange = function () {
            $scope.channels = [{channel: "", channelName: "二级频道"}];
            $scope.search.channelId="";
            $scope.search.pageNo=1;


            $state.go($state.current.name, $scope.search);

        }

        $scope.channelChange = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search);

        }
        $scope.reviewStatusChange = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search);

        }
        $scope.adminChange = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search);
        }


        $scope.keyword = $scope.search.keyword;

        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        }

        $scope.searchClick = function (keyword) {
            $scope.search.keyword = keyword;
            $scope.search.reviewStatus = $scope.search.reviewStatus == 'null' ? null : $scope.search.reviewStatus;
            $state.go($state.current.name, $scope.search)

        }

        $scope.statusClick = function (reviewStatus) {
            $scope.search.keyword = null;

            $scope.search.reviewStatus = status == 'null' ? null : reviewStatus;
            $state.go($state.current.name, $scope.search)

        }

        $scope.noPermission=false;
        AlbumManager.albumList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            if(result.data.c=="n"){
                $scope.noPermission=true;
            }else{
                $scope.noPermission=false;
                $scope.albums = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            }

        })

        $scope.allCheckChange = function (allChecked) {
            $scope.albums.forEach(function (item) {
                item.checked = allChecked;
            })
        }

        $scope.albumCheckChange = function (album) {
            $scope.allChecked = true;
            $scope.albums.forEach(function (item) {
                if (!item.checked) {
                    $scope.allChecked = false;

                }
            })
        }


        $scope.adjustChannel = function (param, elm, size) {
            var ids = [];
            $scope.albums.forEach(function (item) {
                if (item.checked) {
                    ids.push(item.albumId)
                }
            })
            if (!ids.length) {
                return false;
            }
            var modal = $uibModal.open({
                templateUrl: 'adjustChannel.html',
                controller: 'albumAdjustModalController',
                size: 'normal',
                resolve: {
                    param: function () {
                        return {ids: ids, categories: $scope.categories}
                    }
                }
            });
            modal.result.then(function (result) {
                console.log(result)
                if (result == "ok") {
                    setTimeout(function () {
                        $state.reload();
                    }, 200)
                }
            });
        };

        $scope.keyup=function (e,keyword) {

            if(e.key=="Enter"){
                $scope.search.pageNo=1;
                $scope.search.keyword = keyword;
                $scope.search.reviewStatus = $scope.search.reviewStatus == "null" ? null : $scope.search.reviewStatus;
                $state.go($state.current.name, $scope.search);
            }
        }


    }])

